using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DiggApiNet;

namespace DiggApiTestApp
{
    public partial class Form1 : Form
    {
        DiggApi.ListComments    comments    = new DiggApi.ListComments();
        DiggApi.ListEvents      events      = new DiggApi.ListEvents();
        DiggApi.ListStories     stories     = new DiggApi.ListStories();
        DiggApi.ListTopics      topics      = new DiggApi.ListTopics();
        DiggApi.ListUsers       users       = new DiggApi.ListUsers();

        private DateTime min_submit_date    = new DateTime();
        private DateTime max_submit_date    = new DateTime();
        private DateTime min_promote_date   = new DateTime();
        private DateTime max_promote_date   = new DateTime();
        private DateTime max_date           = new DateTime();
        private DateTime min_date           = new DateTime();

        /// <summary>
        /// Class constructor
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            XmlForm xml_form = new XmlForm();
            xml_form.Show();

            DiggApi.AppKey = "http://www.headzoo.com/test";

            comments.RequestPrepare     += new DiggApi.RequestPrepareHandler(xml_form.digg_RequestPrepare);
            events.RequestPrepare       += new DiggApi.RequestPrepareHandler(xml_form.digg_RequestPrepare);
            stories.RequestPrepare      += new DiggApi.RequestPrepareHandler(xml_form.digg_RequestPrepare);
            topics.RequestPrepare       += new DiggApi.RequestPrepareHandler(xml_form.digg_RequestPrepare);
            users.RequestPrepare        += new DiggApi.RequestPrepareHandler(xml_form.digg_RequestPrepare);

            comments.RequestSuccess     += new DiggApi.RequestSuccessHandler(xml_form.digg_RequestSuccess);
            events.RequestSuccess       += new DiggApi.RequestSuccessHandler(xml_form.digg_RequestSuccess);
            stories.RequestSuccess      += new DiggApi.RequestSuccessHandler(xml_form.digg_RequestSuccess);
            topics.RequestSuccess       += new DiggApi.RequestSuccessHandler(xml_form.digg_RequestSuccess);
            users.RequestSuccess        += new DiggApi.RequestSuccessHandler(xml_form.digg_RequestSuccess);
        }

        #region LS
        /// <summary>
        /// Gets a list of all stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetAll_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetAll(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets a list of popular stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetPopular_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetPopular(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets a list of upcoming stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetUpcoming_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetUpcoming(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }
        
        /// <summary>
        /// Gets stories in the specified container
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByContainer_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSContainerName1.Text)) {
                MessageBox.Show("You must enter a container name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByContainer(textBoxLSContainerName1.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets popular stories in the specified container
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByContainerPopular_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSContainerName2.Text)) {
                MessageBox.Show("You must enter a container name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByContainerPopular(textBoxLSContainerName2.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets upcoming stories in the specified container
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByContainerUpcoming_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSContainerName3.Text)) {
                MessageBox.Show("You must enter a container name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByContainerUpcoming(textBoxLSContainerName3.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets stories in the specified topic
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByTopic_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSTopicName1.Text)) {
                MessageBox.Show("You must enter a topic name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByTopic(textBoxLSTopicName1.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }
        
        /// <summary>
        /// Gets popular stories in the specified topic
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByTopicPopular_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSTopicName2.Text)) {
                MessageBox.Show("You must enter a topic name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByTopicPopular(textBoxLSTopicName2.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets upcoming stories in the specified topic
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByTopicUpcoming_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSTopicName3.Text)) {
                MessageBox.Show("You must enter a topic name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByTopicUpcoming(textBoxLSTopicName3.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets a single story with the specified ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetById_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSId.Text)) {
                MessageBox.Show("You must enter a story ID.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStory st;
            try {
                st = stories.GetById(long.Parse(textBoxLSId.Text), args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }
        
        /// <summary>
        /// Gets a list of stories with the specified IDs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByIds_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSIds.Text)) {
                MessageBox.Show("You must enter story IDs separated by spaces.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string[] parts = textBoxLSIds.Text.Split(' ');
            long[] ids = new long[parts.Length];
            for (int i = 0; i < parts.Length; i++) {
                ids[i] = long.Parse(parts[i]);
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByIds(ids, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }
        
        /// <summary>
        /// Gets stories submitted by the specified user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByUser_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSUsername.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStories st;
            try {
                st = stories.GetByUser(textBoxLSUsername.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        /// <summary>
        /// Gets a single story with the specified safe name
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLSGetByTitle_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLSTitle.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("ls");
            DiggStory st;
            try {
                st = stories.GetByTitle(textBoxLSTitle.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLS(st);
        }

        private void AddStoriesToDataGridViewLS(DiggStories stor)
        {
            int i = 0;
            foreach (DiggStory s in stor) {
                dataGridViewLS.Rows.Add();
                dataGridViewLS.Rows[i].Cells["ColumnId"].Value          = s.Id.ToString();
                dataGridViewLS.Rows[i].Cells["ColumnLink"].Value        = s.Link;
                dataGridViewLS.Rows[i].Cells["ColumnSubmitDate"].Value  = s.SubmitDate.ToString("r");
                dataGridViewLS.Rows[i].Cells["ColumnDiggs"].Value       = s.Diggs.ToString();
                dataGridViewLS.Rows[i].Cells["ColumnComments"].Value    = s.Comments.ToString();
                dataGridViewLS.Rows[i].Cells["ColumnHref"].Value        = s.Href;
                dataGridViewLS.Rows[i].Cells["ColumnTitle"].Value       = s.Title;
                dataGridViewLS.Rows[i].Cells["ColumnDescription"].Value = s.Description;
                dataGridViewLS.Rows[i].Cells["ColumnUser"].Value        = s.User.Name;
                dataGridViewLS.Rows[i].Cells["ColumnContainer"].Value   = s.Container.Name;
                dataGridViewLS.Rows[i].Cells["ColumnTopic"].Value       = s.Topic.Name;
                i++;
            }
        }
        private void AddStoriesToDataGridViewLS(DiggStory stor)
        {
            dataGridViewLS.Rows.Add();
            dataGridViewLS.Rows[0].Cells["ColumnId"].Value              = stor.Id.ToString();
            dataGridViewLS.Rows[0].Cells["ColumnLink"].Value            = stor.Link;
            dataGridViewLS.Rows[0].Cells["ColumnSubmitDate"].Value      = stor.SubmitDate.ToString("r");
            dataGridViewLS.Rows[0].Cells["ColumnDiggs"].Value           = stor.Diggs.ToString();
            dataGridViewLS.Rows[0].Cells["ColumnComments"].Value        = stor.Comments.ToString();
            dataGridViewLS.Rows[0].Cells["ColumnHref"].Value            = stor.Href;
            dataGridViewLS.Rows[0].Cells["ColumnTitle"].Value           = stor.Title;
            dataGridViewLS.Rows[0].Cells["ColumnDescription"].Value     = stor.Description;
            dataGridViewLS.Rows[0].Cells["ColumnUser"].Value            = stor.User.Name;
            dataGridViewLS.Rows[0].Cells["ColumnContainer"].Value       = stor.Container.Name;
            dataGridViewLS.Rows[0].Cells["ColumnTopic"].Value           = stor.Topic.Name;
        }
        private void buttonLSReset_Click(object sender, EventArgs e)
        {
            ResetParams();
        }
        #endregion

        #region LU
        /// <summary>
        /// Gets all users
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1LUGetAll_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("lu");
            DiggUsers us;
            try {
                us = users.GetAll(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLU(us);
        }

        /// <summary>
        /// Gets data for the specified user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLUGetByUsername_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLUUsername.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("lu");
            DiggUser us;
            try {
                us = users.GetByUsername(textBoxLUUsername.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLU(us);
        }

        /// <summary>
        /// Gets friends of the specified user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLUGetFriends_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLUFriends.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("lu");
            DiggUsers us;
            try {
                us = users.GetFriends(textBoxLUFriends.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLU(us);
        }

        /// <summary>
        /// Gets fans of the specified user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLUGetFans_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLUFans.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("lu");
            DiggUsers us;
            try {
                us = users.GetFans(textBoxLUFans.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLU(us);
        }

        private void AddStoriesToDataGridViewLU(DiggUsers use)
        {
            int i = 0;
            foreach (DiggUser u in use) {
                dataGridViewLU.Rows.Add();
                dataGridViewLU.Rows[i].Cells["ColumnName"].Value = u.Name;
                dataGridViewLU.Rows[i].Cells["ColumnIcon"].Value = u.Icon;
                dataGridViewLU.Rows[i].Cells["ColumnRegistered"].Value = u.Registered.ToString("r");
                dataGridViewLU.Rows[i].Cells["ColumnProfileViews"].Value = u.ProfileViews.ToString();
                i++;
            }
        }
        private void AddStoriesToDataGridViewLU(DiggUser user)
        {
            dataGridViewLU.Rows.Add();
            dataGridViewLU.Rows[0].Cells["ColumnName"].Value = user.Name;
            dataGridViewLU.Rows[0].Cells["ColumnIcon"].Value = user.Icon;
            dataGridViewLU.Rows[0].Cells["ColumnRegistered"].Value = user.Registered.ToString("r");
            dataGridViewLU.Rows[0].Cells["ColumnProfileViews"].Value = user.ProfileViews.ToString();
        }
        private void buttonLUReset_Click(object sender, EventArgs e)
        {
            ResetParams();
        }
        #endregion

        #region LE
        /// <summary>
        /// Gets all events
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetAll_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetAll(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        /// <summary>
        /// Gets events from popular stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetPopular_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetPopular(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        /// <summary>
        /// Gets events for upcoming stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetUpcoming_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetUpcoming(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        /// <summary>
        /// Gets events for a story using the specified story ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetByStoryId_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLEId.Text)) {
                MessageBox.Show("You must enter a story ID.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetByStoryId(long.Parse(textBoxLEId.Text), args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        /// <summary>
        /// Gets events for several stories using the specified story IDs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetByStoryIds_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLEIds.Text)) {
                MessageBox.Show("You must enter story IDs separated by spaces.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string[] parts = textBoxLEIds.Text.Split(' ');
            long[] ids = new long[parts.Length];
            for (int i = 0; i < parts.Length; i++) {
                ids[i] = long.Parse(parts[i]);
            }

            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetByStoryIds(ids, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        /// <summary>
        /// Gets events from the specified user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetByUser_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLEUsername.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetByUser(textBoxLEUsername.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        /// <summary>
        /// Gets events from the specified users
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetByUsers_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLEUsernames.Text)) {
                MessageBox.Show("You must enter a usernames.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetByUsers(textBoxLEUsernames.Text.Split(' '), args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        /// <summary>
        /// Gets events for a story (specified by ID) for the given user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLEGetByStoryIdByUser_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLEId2.Text) || String.IsNullOrEmpty(textBoxLEUsername2.Text)) {
                MessageBox.Show("You must enter a story ID and username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("le");
            DiggEvents ev;
            try {
                ev = events.GetByStoryIdByUser(long.Parse(textBoxLEId2.Text), textBoxLEUsername2.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLE(ev);
        }

        private void AddStoriesToDataGridViewLE(DiggEvents evn)
        {
            int i = 0;
            foreach (DiggEvent e in evn) {
                dataGridViewLE.Rows.Add();
                dataGridViewLE.Rows[i].Cells["ColumnLEID"].Value = e.Id.ToString();
                dataGridViewLE.Rows[i].Cells["ColumnStory"].Value = e.Story.ToString();
                dataGridViewLE.Rows[i].Cells["ColumnDate"].Value = e.Date.ToString("r");
                dataGridViewLE.Rows[i].Cells["ColumnLEUser"].Value = e.User;
                dataGridViewLE.Rows[i].Cells["ColumnStatus"].Value = e.Status;
                i++;
            }
        }
        private void AddStoriesToDataGridViewLE(DiggEvent even)
        {
            dataGridViewLU.Rows.Add();
            dataGridViewLE.Rows[0].Cells["ColumnLEId"].Value = even.Id.ToString();
            dataGridViewLE.Rows[0].Cells["ColumnStory"].Value = even.Story.ToString();
            dataGridViewLE.Rows[0].Cells["ColumnDate"].Value = even.Date.ToString("r");
            dataGridViewLE.Rows[0].Cells["ColumnLEUser"].Value = even.User;
            dataGridViewLE.Rows[0].Cells["ColumnStatus"].Value = even.Status;
        }
        private void buttonLEReset_Click(object sender, EventArgs e)
        {
            ResetParams();
        }
        #endregion

        #region LC
        /// <summary>
        /// Gets all comments
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLCGetAll_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("lc");
            DiggComments com;
            try {
                com = comments.GetAll(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLC(com);
        }

        /// <summary>
        /// Gets comments for popular stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLCGetPopular_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("lc");
            DiggComments com;
            try {
                com = comments.GetPopular(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLC(com);
        }

        /// <summary>
        /// Gets comments for upcoming stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLCGetUpcoming_Click(object sender, EventArgs e)
        {
            Hashtable args = BuildRequestArgs("lc");
            DiggComments com;
            try {
                com = comments.GetUpcoming(args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLC(com);
        }

        /// <summary>
        /// Gets comments for the specified story
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLCGetByStoryId_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLCId.Text)) {
                MessageBox.Show("You must enter a story ID.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("lc");
            DiggComments com;
            try {
                com = comments.GetByStoryId(long.Parse(textBoxLCId.Text), args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLC(com);
        }

        /// <summary>
        /// Gets comments for the specified stories
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLCGetByStoryIds_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLCIds.Text)) {
                MessageBox.Show("You must enter story IDs separated by spaces.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string[] parts = textBoxLCIds.Text.Split(' ');
            long[] ids = new long[parts.Length];
            for (int i = 0; i < parts.Length; i++) {
                ids[i] = long.Parse(parts[i]);
            }

            Hashtable args = BuildRequestArgs("lc");
            DiggComments com;
            try {
                com = comments.GetByStoryIds(ids, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLC(com);
        }

        /// <summary>
        /// Gets comments by the specified user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLCGetByUser_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLCUsername.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("lc");
            DiggComments com;
            try {
                com = comments.GetByUser(textBoxLCUsername.Text, args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLC(com);
        }

        /// <summary>
        /// Gets comments by the specified users
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLCGetByUsers_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLCUsernames.Text)) {
                MessageBox.Show("You must enter a username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Hashtable args = BuildRequestArgs("lc");
            DiggComments com;
            try {
                com = comments.GetByUsers(textBoxLCUsernames.Text.Split(' '), args);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLC(com);
        }

        private void AddStoriesToDataGridViewLC(DiggComments com)
        {
            int i = 0;
            foreach (DiggComment c in com) {
                dataGridViewLC.Rows.Add();
                dataGridViewLC.Rows[i].Cells["ColumnLCDate"].Value = c.Date.ToString("r");
                dataGridViewLC.Rows[i].Cells["ColumnLCStory"].Value = c.Story.ToString();
                dataGridViewLC.Rows[i].Cells["ColumnLCId"].Value = c.Id.ToString();
                dataGridViewLC.Rows[i].Cells["ColumnLCUP"].Value = c.Up.ToString();
                dataGridViewLC.Rows[i].Cells["ColumnLCDown"].Value = c.Down.ToString();
                dataGridViewLC.Rows[i].Cells["ColumnLCReplies"].Value = c.Replies.ToString();
                dataGridViewLC.Rows[i].Cells["ColumnLCReplyTo"].Value = c.ReplyTo.ToString();
                dataGridViewLC.Rows[i].Cells["ColumnLCUser"].Value = c.User;
                dataGridViewLC.Rows[i].Cells["ColumnLCComment"].Value = c.Comment;
                i++;
            }
        }
        private void AddStoriesToDataGridViewLC(DiggComment comm)
        {
            dataGridViewLC.Rows.Add();
            dataGridViewLC.Rows[0].Cells["ColumnLCDate"].Value = comm.Date.ToString("r");
            dataGridViewLC.Rows[0].Cells["ColumnLCStory"].Value = comm.Story.ToString();
            dataGridViewLC.Rows[0].Cells["ColumnLCId"].Value = comm.Id.ToString();
            dataGridViewLC.Rows[0].Cells["ColumnLCUP"].Value = comm.Up.ToString();
            dataGridViewLC.Rows[0].Cells["ColumnLCDown"].Value = comm.Down.ToString();
            dataGridViewLC.Rows[0].Cells["ColumnLCReplies"].Value = comm.Replies.ToString();
            dataGridViewLC.Rows[0].Cells["ColumnLCReplyTo"].Value = comm.ReplyTo.ToString();
            dataGridViewLC.Rows[0].Cells["ColumnLCUser"].Value = comm.User;
            dataGridViewLC.Rows[0].Cells["ColumnLCComment"].Value = comm.Comment;
        }
        private void buttonLCReset_Click(object sender, EventArgs e)
        {
            ResetParams();
        }
        #endregion

        #region LT
        /// <summary>
        /// Get all topics
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLTGetAll_Click(object sender, EventArgs e)
        {
            DiggTopics top;
            try {
                top = topics.GetAll();
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLT(top);
        }

        /// <summary>
        /// Get data on the specified topic
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLTGetByShortName_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBoxLTShortName.Text)) {
                MessageBox.Show("You must enter a topic short name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DiggTopic top;
            try {
                top = topics.GetByShortName(textBoxLTShortName.Text);
            }
            catch (DiggApiException ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            AddStoriesToDataGridViewLT(top);
        }

        private void AddStoriesToDataGridViewLT(DiggTopics top)
        {
            int i = 0;
            foreach (DiggTopic t in top) {
                dataGridViewLT.Rows.Add();
                dataGridViewLT.Rows[i].Cells["ColumnLTName"].Value = t.Name;
                dataGridViewLT.Rows[i].Cells["ColumnLTShortName"].Value = t.ShortName;
                i++;
            }
        }
        private void AddStoriesToDataGridViewLT(DiggTopic top)
        {
            dataGridViewLT.Rows.Add();
            dataGridViewLT.Rows[0].Cells["ColumnLTName"].Value = top.Name;
            dataGridViewLT.Rows[0].Cells["ColumnLTShortName"].Value = top.ShortName;
        }
        #endregion

        /// <summary>
        /// Resets the request parameter form values on all tabs, and the DataGridViews.
        /// </summary>
        private void ResetParams()
        {
            dateTimePickerLSMinSubmitDate.Value = DateTime.Now;
            dateTimePickerLSMaxSubmitDate.Value = DateTime.Now;
            dateTimePickerLSMinPromoteDate.Value = DateTime.Now;
            dateTimePickerLSMaxPromoteDate.Value = DateTime.Now;
            dateTimePickerLEMinDate.Value = DateTime.Now;
            dateTimePickerLEMaxDate.Value = DateTime.Now;
            dateTimePickerLCMinDate.Value = DateTime.Now;
            dateTimePickerLCMaxDate.Value = DateTime.Now;

            max_submit_date     = new DateTime();
            min_submit_date     = new DateTime();
            max_promote_date    = new DateTime();
            min_promote_date    = new DateTime();
            min_date            = new DateTime();
            max_date            = new DateTime();

            comboBoxLSSort.SelectedIndex = -1;
            comboBoxLCSort.SelectedIndex = -1;
            comboBoxLESort.SelectedIndex = -1;
            comboBoxLUSort.SelectedIndex = -1;

            textBoxLSOffset.Text = "";
            textBoxLSCount.Text = "";
            textBoxLSDomain.Text = "";
            textBoxLSLink.Text = "";
            textBoxLUOffset.Text = "";
            textBoxLUCount.Text = "";
            textBoxLEOffset.Text = "";
            textBoxLECount.Text = "";
            textBoxLCOffset.Text = "";
            textBoxLCCount.Text = "";

            dataGridViewLS.Rows.Clear();
            dataGridViewLU.Rows.Clear();
            dataGridViewLE.Rows.Clear();
            dataGridViewLC.Rows.Clear();
            dataGridViewLT.Rows.Clear();
        }

        /// <summary>
        /// Builds a Hashtable of arguments to be passed with each Digg request.
        /// </summary>
        /// <param name="which">Build arguments for which tab: "ls", "lu", "le", and "lc"</param>
        /// <returns>A Hashtable of arguments</returns>
        private Hashtable BuildRequestArgs(string which)
        {
            /**
             * Arguments can be passed with each request to Digg. For instance arguments
             * that specify how many stories to return (count), how to order the returned
             * users list (username-asc, username-desc), etc. Every DiggApi method accepts
             * a Hashtable as the last argument, although different arguments are used by
             * different methods. Check the Digg API docs for more info.
             */
            Hashtable table = new Hashtable();
            switch (which) {
                // Building args from the ListStories tab
                case "ls":
                if (min_submit_date.ToString("d") != "1/1/0001") {
                    table.Add("min_submit_date", min_submit_date);
                }
                if (max_submit_date.ToString("d") != "1/1/0001") {
                    table.Add("max_submit_date", max_submit_date);
                }
                if (min_promote_date.ToString("d") != "1/1/0001") {
                    table.Add("min_promote_date", min_promote_date);
                }
                if (max_promote_date.ToString("d") != "1/1/0001") {
                    table.Add("max_promote_date", max_promote_date);
                }
                if (comboBoxLSSort.SelectedIndex != -1) {
                    table.Add("sort", comboBoxLSSort.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLSOffset.Text)) {
                    table.Add("offset", textBoxLSOffset.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLSCount.Text)) {
                    table.Add("count", textBoxLSCount.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLSDomain.Text)) {
                    table.Add("domain", textBoxLSDomain.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLSLink.Text)) {
                    table.Add("link", textBoxLSLink.Text);
                }
                break;
                
                // Building args for the ListUsers tab
                case "lu":
                if (comboBoxLUSort.SelectedIndex != -1) {
                    table.Add("sort", comboBoxLUSort.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLUOffset.Text)) {
                    table.Add("offset", textBoxLUOffset.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLUCount.Text)) {
                    table.Add("count", textBoxLUCount.Text);
                }
                break;
                
                // Building args for the ListEvents tab
                case "le":
                if (min_date.ToString("d") != "1/1/0001") {
                    table.Add("min_date", min_date);
                }
                if (max_date.ToString("d") != "1/1/0001") {
                    table.Add("max_date", max_date);
                }
                if (comboBoxLESort.SelectedIndex != -1) {
                    table.Add("sort", comboBoxLESort.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLEOffset.Text)) {
                    table.Add("offset", textBoxLEOffset.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLECount.Text)) {
                    table.Add("count", textBoxLECount.Text);
                }
                break;

                // Building args for the ListComments tab
                case "lc":
                if (min_date.ToString("d") != "1/1/0001") {
                    table.Add("min_date", min_date);
                }
                if (max_date.ToString("d") != "1/1/0001") {
                    table.Add("max_date", max_date);
                }
                if (comboBoxLCSort.SelectedIndex != -1) {
                    table.Add("sort", comboBoxLCSort.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLCOffset.Text)) {
                    table.Add("offset", textBoxLCOffset.Text);
                }
                if (!String.IsNullOrEmpty(textBoxLCCount.Text)) {
                    table.Add("count", textBoxLCCount.Text);
                }
                break;
            }

            return table;
        }

        #region Date Selections
        private void dateTimePickerLSMinSubmitDate_ValueChanged(object sender, EventArgs e)
        {
            min_submit_date = dateTimePickerLSMinSubmitDate.Value;
        }

        private void dateTimePickerLSMaxSubmitDate_ValueChanged(object sender, EventArgs e)
        {
            max_submit_date = dateTimePickerLSMaxSubmitDate.Value;
        }

        private void dateTimePickerLSMinPromoteDate_ValueChanged(object sender, EventArgs e)
        {
            min_promote_date = dateTimePickerLSMinPromoteDate.Value;
        }

        private void dateTimePickerLSMaxPromoteDate_ValueChanged(object sender, EventArgs e)
        {
            max_promote_date = dateTimePickerLSMaxPromoteDate.Value;
        }

        private void dateTimePickerLEMinDate_ValueChanged(object sender, EventArgs e)
        {
            min_date = dateTimePickerLEMinDate.Value;
        }

        private void dateTimePickerLEMaxDate_ValueChanged(object sender, EventArgs e)
        {
            max_date = dateTimePickerLEMaxDate.Value;
        }

        private void dateTimePickerLCMinDate_ValueChanged(object sender, EventArgs e)
        {
            min_date = dateTimePickerLCMinDate.Value;
        }

        private void dateTimePickerLCMaxDate_ValueChanged(object sender, EventArgs e)
        {
            max_date = dateTimePickerLCMaxDate.Value;
        }
        #endregion
    }
}